<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>🔍 Phase 1: 基础连接测试</title>
    <style>
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
            margin: 20px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            color: #333;
        }
        
        .container {
            max-width: 800px;
            margin: 0 auto;
            background: white;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.2);
            padding: 30px;
        }
        
        .header {
            text-align: center;
            margin-bottom: 30px;
            padding-bottom: 20px;
            border-bottom: 2px solid #eee;
        }
        
        .status-grid {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 20px;
            margin-bottom: 30px;
        }
        
        .service-card {
            border: 2px solid #e3e3e3;
            border-radius: 10px;
            padding: 20px;
            text-align: center;
            transition: all 0.3s ease;
        }
        
        .service-card.online {
            border-color: #28a745;
            background: #f8fff9;
        }
        
        .service-card.offline {
            border-color: #dc3545;
            background: #fff8f8;
        }
        
        .service-card.testing {
            border-color: #ffc107;
            background: #fffef8;
        }
        
        .status-indicator {
            font-size: 24px;
            margin-bottom: 10px;
        }
        
        .test-buttons {
            display: flex;
            flex-direction: column;
            gap: 10px;
            margin-bottom: 20px;
        }
        
        .btn {
            padding: 12px 24px;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            font-weight: 500;
            transition: all 0.3s ease;
        }
        
        .btn-primary {
            background: #007bff;
            color: white;
        }
        
        .btn-primary:hover {
            background: #0056b3;
            transform: translateY(-2px);
        }
        
        .btn-success {
            background: #28a745;
            color: white;
        }
        
        .btn-success:hover {
            background: #1e7e34;
        }
        
        .log-area {
            background: #f8f9fa;
            border: 1px solid #dee2e6;
            border-radius: 8px;
            padding: 15px;
            max-height: 300px;
            overflow-y: auto;
            font-family: 'Courier New', monospace;
            font-size: 12px;
            line-height: 1.4;
        }
        
        .log-entry {
            margin-bottom: 5px;
            padding: 2px 0;
        }
        
        .log-info { color: #0066cc; }
        .log-success { color: #00aa00; }
        .log-error { color: #cc0000; }
        
        .overall-status {
            text-align: center;
            margin: 20px 0;
            padding: 15px;
            border-radius: 10px;
            font-weight: bold;
            font-size: 18px;
        }
        
        .overall-status.ready {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        
        .overall-status.not-ready {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        
        .overall-status.testing {
            background: #fff3cd;
            color: #856404;
            border: 1px solid #ffeaa7;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>🔍 Phase 1: 基础连接测试</h1>
            <p>验证智能通知系统服务连接状态</p>
        </div>

        <div class="status-grid">
            <div class="service-card" id="mainServiceCard">
                <div class="status-indicator" id="mainServiceIcon">⭕</div>
                <h3>主通知服务</h3>
                <p><strong>http://localhost:48081</strong></p>
                <p id="mainServiceStatus">等待测试...</p>
                <p id="mainServiceDetails"></p>
            </div>

            <div class="service-card" id="mockServiceCard">
                <div class="status-indicator" id="mockServiceIcon">⭕</div>
                <h3>Mock School API</h3>
                <p><strong>http://localhost:48082</strong></p>
                <p id="mockServiceStatus">等待测试...</p>
                <p id="mockServiceDetails"></p>
            </div>
        </div>

        <div class="overall-status testing" id="overallStatus">
            🔄 准备开始连接测试...
        </div>

        <div class="test-buttons">
            <button class="btn btn-primary" onclick="testMainService()">
                🔧 测试主通知服务 (48081)
            </button>
            <button class="btn btn-primary" onclick="testMockService()">
                🔧 测试Mock School API (48082)
            </button>
            <button class="btn btn-success" onclick="testBothServices()">
                🚀 测试所有服务
            </button>
        </div>

        <div>
            <h3>📋 测试日志</h3>
            <div class="log-area" id="logArea">
                <div class="log-entry log-info">[INFO] Phase 1 基础连接测试页面已加载</div>
                <div class="log-entry log-info">[INFO] 等待用户开始测试...</div>
            </div>
        </div>
    </div>

    <script>
        // 全局状态
        const testState = {
            mainService: false,
            mockService: false,
            logs: []
        };

        // 添加日志
        function addLog(message, type = 'info') {
            const timestamp = new Date().toLocaleTimeString();
            const logEntry = `[${timestamp}] [${type.toUpperCase()}] ${message}`;
            testState.logs.push({ message: logEntry, type });
            
            const logArea = document.getElementById('logArea');
            const logDiv = document.createElement('div');
            logDiv.className = `log-entry log-${type}`;
            logDiv.textContent = logEntry;
            logArea.appendChild(logDiv);
            logArea.scrollTop = logArea.scrollHeight;
            
            console.log(logEntry);
        }

        // 更新服务状态显示
        function updateServiceStatus(service, isOnline, details = '') {
            const cardId = `${service}ServiceCard`;
            const iconId = `${service}ServiceIcon`;
            const statusId = `${service}ServiceStatus`;
            const detailsId = `${service}ServiceDetails`;
            
            const card = document.getElementById(cardId);
            const icon = document.getElementById(iconId);
            const status = document.getElementById(statusId);
            const detailsEl = document.getElementById(detailsId);
            
            if (isOnline) {
                card.className = 'service-card online';
                icon.textContent = '✅';
                status.textContent = '服务在线';
                status.style.color = '#28a745';
            } else {
                card.className = 'service-card offline';
                icon.textContent = '❌';
                status.textContent = '服务离线';
                status.style.color = '#dc3545';
            }
            
            detailsEl.textContent = details;
            testState[`${service}Service`] = isOnline;
            updateOverallStatus();
        }

        // 设置测试中状态
        function setTestingStatus(service) {
            const cardId = `${service}ServiceCard`;
            const iconId = `${service}ServiceIcon`;
            const statusId = `${service}ServiceStatus`;
            
            document.getElementById(cardId).className = 'service-card testing';
            document.getElementById(iconId).textContent = '🔄';
            document.getElementById(statusId).textContent = '测试中...';
            document.getElementById(statusId).style.color = '#856404';
        }

        // 更新整体状态
        function updateOverallStatus() {
            const overallEl = document.getElementById('overallStatus');
            
            if (testState.mainService && testState.mockService) {
                overallEl.className = 'overall-status ready';
                overallEl.textContent = '🎉 所有服务就绪！可以进行下一阶段测试';
            } else if (testState.mainService || testState.mockService) {
                overallEl.className = 'overall-status not-ready';
                overallEl.textContent = '⚠️ 部分服务异常，请检查服务状态';
            } else {
                overallEl.className = 'overall-status not-ready';
                overallEl.textContent = '❌ 服务连接失败，请检查服务是否启动';
            }
        }

        // 测试主服务
        async function testMainService() {
            addLog('开始测试主通知服务连接...');
            setTestingStatus('main');
            
            try {
                const response = await fetch('http://localhost:48081/admin-api/test/notification/api/ping', {
                    method: 'GET',
                    headers: {
                        'Content-Type': 'application/json',
                        'tenant-id': '1'
                    }
                });

                if (response.ok) {
                    const data = await response.text();
                    addLog(`主服务连接成功: ${data}`, 'success');
                    updateServiceStatus('main', true, `响应: ${data.substring(0, 50)}...`);
                } else {
                    addLog(`主服务连接失败: HTTP ${response.status}`, 'error');
                    updateServiceStatus('main', false, `HTTP ${response.status}`);
                }
            } catch (error) {
                addLog(`主服务连接异常: ${error.message}`, 'error');
                updateServiceStatus('main', false, error.message);
            }
        }

        // 测试Mock服务
        async function testMockService() {
            addLog('开始测试Mock School API连接...');
            setTestingStatus('mock');
            
            try {
                const response = await fetch('http://localhost:48082/mock-school-api/auth/health', {
                    method: 'GET',
                    headers: {
                        'Content-Type': 'application/json'
                    }
                });

                if (response.ok) {
                    const data = await response.json();
                    addLog(`Mock API连接成功: ${data.message}`, 'success');
                    updateServiceStatus('mock', true, `消息: ${data.message}`);
                } else {
                    addLog(`Mock API连接失败: HTTP ${response.status}`, 'error');
                    updateServiceStatus('mock', false, `HTTP ${response.status}`);
                }
            } catch (error) {
                addLog(`Mock API连接异常: ${error.message}`, 'error');
                updateServiceStatus('mock', false, error.message);
            }
        }

        // 测试所有服务
        async function testBothServices() {
            addLog('========== 开始全面连接测试 ==========');
            document.getElementById('overallStatus').className = 'overall-status testing';
            document.getElementById('overallStatus').textContent = '🔄 正在测试所有服务...';
            
            // 并行测试两个服务
            await Promise.all([testMainService(), testMockService()]);
            
            addLog('========== 连接测试完成 ==========');
        }

        // 页面加载完成后自动进行一次测试
        window.addEventListener('DOMContentLoaded', function() {
            addLog('页面加载完成，准备进行初始连接测试');
            setTimeout(() => {
                testBothServices();
            }, 1000);
        });
    </script>
</body>
</html>